<template>
  <div class="app-container">
    <el-form
      ref="ruleForm"
      :inline="true"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-card class="box-card">
        <div>
          <el-form-item
            label="标题："
            prop="title"
          >
            <el-input
              v-model="ruleForm.title"
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item
            label="消息类型："
            prop="messageType"
          >
            <el-select
              v-model="ruleForm.messageType"
              clearable
              size="small"
              placeholder="请选择"
            >
              <el-option
                v-for="item in dict.message_type"
                :key="item.id"
                :value="item.value"
                :label="item.label"
              />
            </el-select>
          </el-form-item>
        </div>
      </el-card>
      <el-card style="margin-top: 50px;">
        <div
          slot="header"
          class="clearfix"
        >
          <span>内容</span>
        </div>
        <el-form-item prop="content">
          <div ref="editor" class="editor" />
        </el-form-item>
      </el-card>
      <el-form-item style="margin-top: 10px">
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
        >确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import WangEditor from '@/components/WangEditor/index'
import { addInfo } from '@/api/info'
import { upload } from '@/utils/upload'
import E from 'wangeditor'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'

export default {
  name: 'AddInfo',
  // components: { WangEditor },
  dicts: ['message_type'],
  data() {
    return {
      ruleForm: {
        title: '',
        content: '',
        messageType: ''
      },
      fileList: [],
      headers: {
        'Authorization': getToken()
      },
      rules: {
        title: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        messageType: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    }
  },

  created() {

  },
  computed: {
    ...mapGetters([
      'imagesUploadApi',
      'baseApi'
    ])
  },
  mounted() {
    const _this = this
    var editor = new E(this.$refs.editor)
    // 自定义菜单配置
    editor.config.zIndex = 10
    // 文件上传
    editor.config.customUploadImg = function(files, insert) {
      // files 是 input 中选中的文件列表
      // insert 是获取图片 url 后，插入到编辑器的方法
      files.forEach(image => {
        upload(_this.imagesUploadApi, image).then(res => {
          const data = res.data
          const url = _this.baseApi + '/file/' + data.type + '/' + data.realName
          insert(url)
        })
      })
    }
    editor.config.onchange = (html) => {
      this.ruleForm.content = html
    }
    editor.create()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          await addInfo(this.ruleForm)
          this.$router.go(-1)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancel() {
      this.$refs.ruleForm.resetFields()
      this.$router.go(-1)
    }
  }
}
</script>

    <style rel="stylesheet/scss" lang="scss" scoped>
    .app-container {
      height: calc(100vh - 50px);
      overflow: auto;
    }
    ::v-deep(.el-button--text) {
      border: 0;
      padding: 0;
      width: 60px;
      background-color: #fff;
      color: #1890ff;
    }
    .edit {
      z-index: 9999;
      position: relative;
    }
    </style>

